package cn.ticket.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ticket.model.Buses;
import cn.ticket.model.Busorder;
import cn.ticket.service.BusOrderService;
import cn.ticket.service.BusesService;
import cn.ticket.service.Utility;

@Controller
@RequestMapping("buses")
public class BusesController {
@Autowired
BusesService busesService;
@Autowired 
private BusOrderService busOrderService;
@RequestMapping(value="insert",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String insert(@RequestBody Buses record){
	Map<String, Object> result=new HashMap<>();
	record.setAdddate(new Date());
	int num = busesService.insertSelective(record);
	if (num==1) {
		Busorder busorder=busOrderService.selectByPrimaryKey(record.getBusorderid());
		busorder.setBusescount((byte) (busorder.getBusescount()+1));
		busorder.setSeatscount((short) (busorder.getSeatscount()+record.getSeatcount()));
		busOrderService.updateByPrimaryKeySelective(busorder);	
		result.put("status", 1001);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
};
@RequestMapping(value="update",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String update(@RequestBody Buses record){
	Map<String, Object> result=new HashMap<>();
	Buses buses=busesService.selectByPrimaryKey(record.getId());
	record.setOccupation(buses.getOccupation());
	int num = busesService.updateByPrimaryKeySelective(record);
	if (num==1) {
		Busorder busorder=busOrderService.selectByPrimaryKey(record.getBusorderid());
		busorder.setSeatscount((short) (busorder.getSeatscount()+record.getSeatcount()-buses.getSeatcount()));
		busOrderService.updateByPrimaryKeySelective(busorder);	
		result.put("status", 1001);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
};
@RequestMapping(value="delete",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String delete(@RequestBody Map<String, String> args){
	Map<String, Object> result=new HashMap<>();
	int id=Integer.parseInt(args.get("id"));
	Buses buses=busesService.selectByPrimaryKey(id);
	int num = busesService.deleteByPrimaryKey(id);
	if (num==1) {
		Busorder busorder=busOrderService.selectByPrimaryKey(buses.getBusorderid());
		busorder.setBusescount((byte) (busorder.getBusescount()-1));
		busorder.setSeatscount((short) (busorder.getSeatscount()-buses.getSeatcount()));
		busOrderService.updateByPrimaryKeySelective(busorder);	
		result.put("status", 1001);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
};
@RequestMapping(value="getAll",produces={"application/json;charset=utf-8"})
@ResponseBody
public String getAll(){
	Map<String, Object> result=new HashMap<>();
	List<Buses> buses= busesService.getAll();
	if (!buses.isEmpty()) {
		result.put("status", 1001);
		result.put("body", buses);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
};
@RequestMapping(value="getByBusOrderId",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String getByBusOrderId(@RequestBody Map<String, String> args){
	Map<String, Object> result=new HashMap<>();
	int busOrderId=Integer.parseInt(args.get("busOrderId"));
	List<Buses> buses= busesService.getByBusOrderId(busOrderId);
	if (!buses.isEmpty()) {
		result.put("status", 1001);
		result.put("body", buses);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
};
@RequestMapping(value="getNearestBuses0",produces={"application/json;charset=utf-8"})
@ResponseBody
public String getNearestBuses0(){
	Map<String, Object> result=new HashMap<>();
	List<Map<String, Object>> buses= busesService.getNearestBuses0();
	if (buses!=null&&!buses.isEmpty()) {
		result.put("status", 1001);
		result.put("body", buses);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
};
@RequestMapping(value="getNearestBuses1",produces={"application/json;charset=utf-8"})
@ResponseBody
public String getNearestBuses1(){
	Map<String, Object> result=new HashMap<>();
	List<Map<String, Object>> buses= busesService.getNearestBuses1();
	if (buses!=null&&!buses.isEmpty()) {
		result.put("status", 1001);
		result.put("body", buses);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
};
@RequestMapping(value="getInfo",produces={"application/json;charset=utf-8"})
@ResponseBody
public String getInfo(){
	Map<String, Object> result=new HashMap<>();
	//int busOrderId=Integer.parseInt(request.getParameter("busOrderId"));
	List<Map<String, Object>> buses= busesService.getInfo();
	if (!buses.isEmpty()) {
		result.put("status", 1001);
		result.put("body", buses);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
};
@RequestMapping(value="getInfoByCount",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String getInfoByCount(@RequestBody Map<String, String> args){
	Map<String, Object> result=new HashMap<>();
	int start=Integer.parseInt(args.get("start"));
	int count=Integer.parseInt(args.get("count"));
	List<Map<String, Object>> buses= busesService.getInfoByCount(start, count);
	if (!buses.isEmpty()) {
		result.put("status", 1001);
		result.put("body", buses);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
};

@RequestMapping(value="getInfoByBusOrderId",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String getInfoByBusOrderId(@RequestBody Map<String, String> args){
	Map<String, Object> result=new HashMap<>();
	int busOrderId=Integer.parseInt(args.get("busOrderId"));
	List<Map<String, Object>> buses= busesService.getInfoByBusOrderId(busOrderId);
	if (!buses.isEmpty()) {
		result.put("status", 1001);
		result.put("body", buses);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
};

}
